﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using CoolCode;

namespace CoolCode.Web.Mvc 
{
	public static class JavaScriptExtensions
	{
		public static MvcHtmlString JavaScript(this HtmlHelper helper, string scripts )
		{ 
			var builder = new TagBuilder("script");
			builder.Attributes.Add("type", "text/javascript");
			builder.InnerHtml = scripts;
		  	string scriptString = builder.ToString(TagRenderMode.Normal);
			MvcHtmlString mvcHtml = MvcHtmlString.Create(scriptString);
			return mvcHtml;
		}

		public static MvcHtmlString JavaScript(this HtmlHelper helper, object args)
		{ 
			StringBuilder scriptBuilder = new StringBuilder();
			if (args != null)
			{
				foreach (var p in args.GetType().GetProperties())
				{
					scriptBuilder.AppendFormat("var {0} = {1};", p.Name, p.GetValue(args, null));
				}
			}
			return JavaScript(helper , scriptBuilder.ToString());
		}
	}
}
